home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / LHX / UTIL.C < prev   
C/C++ Source or Header  |  1992-12-08  |  5KB  |  251 lines

  1. /***********************************************************
  2.         util.c -- utility routines
  3. ***********************************************************/
  4. #include    "lh386.h"
  5. #include    <fslib.h>
  6.  
  7. #include    <stdio.h>
  8. #ifndef __HIGHC__
  9. #    include    <conio.h>
  10. #endif
  11. #include    <io.h>
  12. #include    <string.h>
  13. #include    <dos.h>
  14. #include    <ctype.h>
  15. #include    <time.h>
  16. #include    <stdlib.h>
  17. #include    "lh.h"
  18. #include    "intrface.h"
  19. #include    "errmes.h"
  20.  
  21. #ifdef    __HIGHC__
  22. #    pragma    On(Align_labels);
  23. #endif
  24.  
  25.  
  26. uchar        pathdelim = '\\';
  27. uchar        swchar;
  28. boolean     copy_mode = FALSE;
  29.  
  30. /***************************************
  31.         convert path delimiter
  32. ****************************************
  33.         returns *filename
  34. ***************************************/
  35. char       *convdelim(char *path, char delim)
  36. {
  37.     uchar        c;
  38.     uchar       *p;
  39.     int         kflg = 0;
  40.  
  41.     for (p = (uchar *) path; (c = *p) != 0; p++)
  42.     {
  43.         if (kflg)
  44.         {
  45.             kflg = 0;
  46.         } else if (iskanji(c))
  47.         {
  48.             kflg = 1;
  49.         } else if (c == '\\' || c == DELIM || c == DELIM2)
  50.         {
  51.             *p = delim;
  52.             path = (char *) p + 1;
  53.         }
  54.     }
  55.     return path;
  56. }
  57.  
  58. char       *getfilename(char *path)
  59. {
  60.     char       *q;
  61.  
  62.     if ((q = strrchr(path, DELIM)) != NULL ||
  63.         (q = strchr(path, ':')) != NULL)
  64.     {
  65.         return q + 1;
  66.     } else
  67.     {
  68.         return path;
  69.     }
  70. }
  71.  
  72. /*******************************
  73.   get back to parent directory
  74. *******************************/
  75. char       *backpath(char *p)
  76. {
  77.     char       *q;
  78.  
  79.     q = getfilename(p);
  80.     *q = '\0';
  81.     return q;
  82. }
  83.  
  84. /*******************************
  85.                 ask 'Y' or 'N'
  86. *******************************/
  87. int         getyn(void)
  88. {
  89.     int         yn;
  90.  
  91.     do
  92.     {
  93.         yn = LHX_getch();
  94.         yn = toupper(yn);
  95.         if ( yn == '\x03' )
  96.             error(CTRLBRK, NULL);
  97.     } while (yn != 'Y' && yn != 'N');
  98.     LHX_fprintf(stderr, "%c\n", yn);
  99.     return yn;
  100. }
  101.  
  102. void        getswchar(void)
  103. {
  104.     union REGS    regs;
  105.  
  106.     regs.x.ax = 0x3700;
  107.     intdos(®s, ®s);
  108.     swchar = regs.h.dl;
  109.     if (swchar != '/')
  110.     {
  111.         pathdelim = '/';
  112.     }
  113. }
  114.  
  115. time_t        dos2unix(struct ftime * ft)
  116. {
  117.     struct tm    tm;
  118.  
  119.     tm.tm_sec = ft->ft_tsec * 2;
  120.     tm.tm_min = ft->ft_min;
  121.     tm.tm_hour = ft->ft_hour;
  122.     tm.tm_mday = ft->ft_day;
  123.     tm.tm_mon = ft->ft_month - 1;
  124.     tm.tm_year = ft->ft_year + 80;
  125. #ifndef __HIGHC__
  126.     tm.tm_isdst = timezone;
  127. #else
  128.     tm.tm_isdst = -9 * 3600;
  129. #endif
  130.  
  131.     return mktime(&tm);
  132. }
  133.  
  134. struct ftime *unix2dos(time_t utc)
  135. {
  136.     static struct ftime ft;
  137.     struct tm  *tm;
  138.  
  139.     tm = localtime(&utc);
  140.     ft.ft_tsec = tm->tm_sec / 2;
  141.     ft.ft_min = tm->tm_min;
  142.     ft.ft_hour = tm->tm_hour;
  143.     ft.ft_day = tm->tm_mday;
  144.     ft.ft_month = tm->tm_mon + 1;
  145.     ft.ft_year = tm->tm_year - 80;
  146.     return &ft;
  147. }
  148.  
  149. /*******************************
  150.                 ratio * 1000
  151. *******************************/
  152. int         ratio(ulong a, ulong b, int ord)
  153. {
  154.     int         i;
  155.  
  156.     for (i = 0; i < ord && a < 0x19999999; i++)
  157.     {
  158.         a *= 10;                /* while not overflow */
  159.     }                            /* upto 10^ord times */
  160.     for (; i < ord; i++)
  161.     {                            /* the case of overflow */
  162.         b /= 10;
  163.     }
  164.     if (b == 0)
  165.         return 0;                /* if diviser == 0 */
  166.     a += b / 2;                 /* for round up */
  167.     return (a / b);             /* return (a * 1000 / b) */
  168. }
  169.  
  170. #define BUFFERSIZE TEXT_SIZE
  171.  
  172. void        copyfile(FILE * f1, FILE * f2, long size, int crc_flg)
  173. {
  174.     ushort        xsize;
  175.  
  176.     copy_mode = TRUE;
  177.     crc = 0;
  178.     while (size > 0)
  179.     {
  180.         xsize = (size > BUFFERSIZE) ? BUFFERSIZE : size;
  181.         if (fread(text, 1, xsize, f1) != xsize)
  182.         {
  183.             fileerror(RDERR, f1);
  184.         }
  185.         if (f2)
  186.         {
  187. #ifdef    __LHA386__
  188.             if ( f2 == stdout || f2 == stderr )
  189.             {
  190.                 size_t    cnt;
  191.                 char    *s;
  192.  
  193.                 cnt = xsize;
  194.                 s = (char *)text;
  195.                 while ( cnt-- > 0 )
  196.                     LHX_putc( *s++ );
  197.             } else
  198. #endif
  199.             {    if (fwrite(text, 1, xsize, f2) != xsize)
  200.                     fileerror(WTERR, f2);
  201.             }
  202.         }
  203.         if (crc_flg)
  204.         {
  205.             calccrc(text, xsize);
  206.             /* if (flg_n == 0) LHX_fputc('c', stderr);         */
  207.         }
  208.         size -= xsize;
  209.     }
  210.     copy_mode = FALSE;
  211. }
  212.  
  213. void       *e_malloc(uint size)
  214. {
  215.     void       *p;
  216.  
  217.     if ((p = malloc(size)) == NULL)
  218.         error(MEMOVRERR, NULL);
  219.     return p;
  220. }
  221.  
  222. void       *e_realloc(void *buf, int size)
  223. {
  224.     void       *p;
  225.  
  226.     if ((p = realloc(buf, size)) == NULL)
  227.         error(MEMOVRERR, NULL);
  228.     return p;
  229. }
  230.  
  231. FILE       *myeopen(char *path, char *mode, char *errmes)
  232. {
  233.     FILE       *f;
  234.  
  235. /*    LHX_printf("fopen(\x22%s\x22,\x22%s\x22)\n", path, mode );    */
  236.     f = FS_fopen(path, mode);
  237.     if (f == NULL && errmes != NULL)
  238.         error(errmes, path);
  239.     return f;
  240. }
  241.  
  242. FILE       *mywopen(char *path)
  243. {
  244.     return myeopen(path, "wb", MKFILEERR);
  245. }
  246.  
  247. FILE       *myropen(char *path)
  248. {
  249.     return myeopen(path, "rb", NOFILEERR);
  250. }
  251.